package batailleNavale;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author LE DOAN GIANG NGOC
 *
 */
public class PlacementBateau implements Runnable {

	private Joueur j = null;
	private BufferedReader read;
	private PrintWriter write;

	public PlacementBateau(Joueur j, BufferedReader bfr, PrintWriter ptw) {
		super();
		this.j = j;
		this.read = bfr;
		this.write = ptw;
	}

	@Override
	public void run() {
		String x, y, direction;
		boolean vertical;
		int tailleBateau;
		String resultAjoutBateau; 
		int numeroBateau;
		for (int i = 0; i < Constantes.TAILLE_BATEAU.length; i++) {
			numeroBateau = i+1;
			try {
				do {
					tailleBateau = Constantes.TAILLE_BATEAU[i];
					x = read.readLine();
					y = read.readLine();
					direction = read.readLine();
					System.out.println("tailleBateau = "+tailleBateau);
					System.out.println("x = "+x);
					System.out.println("y = "+y);
					System.out.println("direction = "+direction);
					if (direction.equals("v")){
						vertical = Constantes.CREATION_NAVIRE_VERTICAL;
					} else {
						vertical = Constantes.CREATION_NAVIRE_HORIZONTAL;
					}
					resultAjoutBateau = j.getGestionBateau().ajoutBateau(tailleBateau, Integer.valueOf(x), Integer.valueOf(y), vertical, numeroBateau);
					write.println(resultAjoutBateau);
					write.flush();
				} while (!resultAjoutBateau.equals(Constantes.CODE_AJOUT_BATEAU_OK));
			}  catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
